<?php
    include(dirname(__FILE__)."/../../../Dao/db.php");

    class AdGroupService
    {
        var $memberId;
        var $listingType;
        //キャンペーンＩＤ
        var $camp_Id;
        //キャンペーン名
        var $campaignName;
        //広告グループＩＤ
        var $adGr_Id;
        //広告グループ名
        var $adGroupName;
        //ランディングＵＲＬ
        var $landingUrl;
        //キーワード単位で計測・クリエイティブ単位で計測
        var $countUnit;
        //スポンサードサーチ最高入札額
        var $maxCpc;
        //コンテンツマッチ　オン/オフ
        var $contentMatch;
        //コンテンツマッチ最高入札額
        var $contentMaxCpc;
        //掲載オン/オフ
        var $status;

        function AdGroupService()
        {
            session_start();
            $this->memberId = $_SESSION['memberId'];
            $this->listingType = $_GET['listingType'];
            $this->camp_Id = $_GET['campaignId'];
            $this->campaignName = $this->getCampaignName($this->camp_Id);
            $this->adGr_Id = $_GET['adGroupId'];
            $this->adGroupName = $_POST['adgroup_name'];
            $this->landingUrl = $_POST['landing_url'];
            $this->countUnit = $_POST['count_by_creative'];
            $this->maxCpc = $_POST['sponsored_maxbid'];
//            $this->contentMatch = $_POST['content_match'];
            if(isset($_POST['content_maxbid']) && $_POST['content_maxbid'] != "")
            {
                $this->contentMaxCpc = $_POST['content_maxbid'];
            }
            else
            {
                $this->contentMaxCpc = 0;
            }
            $this->status = $_POST['status'];
        }

        function getCampaignName($camp_Id)
        {
            $query = "SELECT  name ";
            $query .= "FROM   campaign ";
            $query .= "WHERE  camp_Id=$camp_Id";
            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            return $row['name'];
        }

         //同期エラーを表示する
        function getErrorMessage()
        {
            $query = "SELECT syncError FROM adgroup ";
            $query .= "WHERE adGr_Id=$this->adGr_Id ";
            $query .= "AND sync=3 ";

            $result = mysql_query($query);
            $row = mysql_fetch_array($result);
            $syncError = trim($row['syncError']);
            if($syncError)
            {
                $errorTable = "<br><table class='tbl' width='100%' border='0' cellpadding='4' cellspacing='0'>";
                $errorTable .= "<tbody><tr>";
                $errorTable .= "<td class='td9' width='150' align='left'>";
                $errorTable .= "<img src='../images/sync_error.gif' border='0'>エラー</td>";
                $errorTable .= "<td class='td4 left'><pre>".$syncError."</pre></td>";
                $errorTable .= "</tr></tbody>";
                $errorTable .= "</table><br>";
            }

            echo $errorTable;
        }

        function addAdGroup()
        {
            //まず、同じ広告グループ名チェック
            $query = "SELECT * FROM adgroup WHERE ";
            $query .= "memberId=$this->memberId AND ";
            $query .= "listingType=$this->listingType AND ";
            $query .= "camp_Id=$this->camp_Id AND ";
            $query .= "name='$this->adGroupName' AND ";
            $query .= "status!=9";
            $result = mysql_query($query);
            //同じ広告グループ名がある場合、新規できません
            if(mysql_fetch_array($result))
            {
                $message = "同じ広告グループ名がある";
            }
            else
            {
                $query = "INSERT INTO adgroup(memberId,listingType,camp_Id,name,landingUrl,countType,";
                $query .= "maxCpc,maxContentCpc,";
                $query .= "status,sync,registDate,lastUpdate) ";
                $query .= "VALUES($this->memberId,";
                $query .= "$this->listingType,";
                $query .= "$this->camp_Id,";
                $query .= "'$this->adGroupName',";
                $query .= "'$this->landingUrl',";
                $query .= "$this->countUnit,";
                $query .= "$this->maxCpc,";
                $query .= "$this->contentMaxCpc,";
                $query .= "$this->status,";
                //同期状態は同期待ち
                $query .= "2,";
                $query .= "now(),now())";

                $result = mysql_query($query);
                if ($result != "1")
                {
                    $message = "登録失敗しました";
                }
                else
                {
                    $message = "登録しました";
                }
            }
            return $message;
        }

        function updateAdGroup()
        {
            //まず、同じ広告グループ名チェック
            $query = "SELECT * FROM adgroup WHERE ";
            $query .= "memberId=$this->memberId AND ";
            $query .= "listingType=$this->listingType AND ";
            $query .= "camp_Id=$this->camp_Id AND ";
            $query .= "name='$this->adGroupName' AND ";
            $query .= "adGr_Id!=$this->adGr_Id AND ";
            $query .= "status!=9";

            $result = mysql_query($query);
            //同じ広告グループ名がある場合、新規できません
            if(mysql_fetch_array($result))
            {
                $message = "同じ広告グループ名がある";
            }
            else
            {
                $query = "UPDATE adgroup SET name='$this->adGroupName',";
                $query .= "landingUrl='$this->landingUrl',";
                $query .= "countType=$this->countUnit,";
                $query .= "maxCpc=$this->maxCpc,";
                $query .= "maxContentCpc=$this->contentMaxCpc,";
                $query .= "status=$this->status,";
                //同期状態は：同期待ち
                $query .= "sync=2,";
                $query .= "lastUpdate=now() WHERE ";
                $query .= "adGr_Id=$this->adGr_Id";

                $result = mysql_query($query);
                if ($result != "1")
                {
                    $message = "更新失敗しました";
                }
                else
                {
                    $message = "更新しました";
                }
            }
            return $message;
        }

        function updateAllAdGroups()
        {
            $adGroupIds = split(",",$_GET['adGroupIds']);

            $ids = "(";
            for($i=0;$i<count($adGroupIds)-1;$i++)
            {
                $ids .=$adGroupIds[$i].",";
            }
            $ids .= $adGroupIds[count($adGroupIds)-1].")";

            $queryHead = "UPDATE adgroup SET ";
            if(isset($_POST['change_url']))
            {
                $queryBody = "landingUrl='$this->landingUrl',";
                $queryBody .= "countType=$this->countUnit,";
            }

            if(isset($_POST['change_sponsored_search']))
            {
                $queryBody .= "maxCpc=$this->maxCpc,";
            }

            if(isset($_POST['change_contents_match']))
            {
                if(isset($_POST['sponsored_maxbid']))
                {
                    $queryBody .="maxContentCpc=$this->contentMaxCpc,";
                }
            }

            if(isset($_POST['change_status']))
            {
                $queryBody .= "status=$this->status,";
            }

            if(isset($queryBody))
            {
                $query = $queryHead.$queryBody;
                $query .= "sync=2,lastUpdate=now() WHERE ";
                $query .= "adGr_Id IN $ids";

                $result = mysql_query($query);
                if ($result != "1")
                {
                    $message = "更新失敗しました";
                }
                else
                {
                    $message = "更新しました";
                }
            }

            return $message;
        }
    }

?>
